package com.hejian.android.a72ggametools.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.hejian.android.a72ggametools.R;
import com.hejian.android.a72ggametools.bean.Convert;
import com.hejian.android.a72ggametools.imageloader.DoubleCache;
import com.hejian.android.a72ggametools.imageloader.ImageCache;
import com.hejian.android.a72ggametools.imageloader.ImageLoader;
import com.hejian.android.a72ggametools.viewholder.ConvertViewHolder;

import java.util.List;

/**
 * Created by Administrator on 2016/1/16.
 */
public class ConvertAdapter extends BaseAdapter{
    private List<Convert> convertList;
    private Context context;
    private ImageCache imageCache = new DoubleCache();
    private ImageLoader imageLoader;
    private ConvertViewHolder convertViewHolder;

    public ConvertAdapter(List<Convert> convertList, Context context) {
        this.convertList = convertList;
        this.context = context;
        imageLoader = ImageLoader.getInstance(context,imageCache);
    }

    @Override
    public int getCount() {
        return convertList.size();
    }

    @Override
    public Object getItem(int position) {
        return convertList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null){
            view = LayoutInflater.from(context).inflate(R.layout.convert_listview_item,null);
            convertViewHolder = new ConvertViewHolder();
            convertViewHolder.imageView = (ImageView) view.findViewById(R.id.convert_list_item_img);
            convertViewHolder.title = (TextView) view.findViewById(R.id.convert_list_item_title);
            convertViewHolder.price = (TextView) view.findViewById(R.id.convert_list_item_price);
            convertViewHolder.remain = (TextView) view.findViewById(R.id.convert_list_item_remain);
            convertViewHolder.button = (Button) view.findViewById(R.id.convert_list_item_button);
            view.setTag(convertViewHolder);
        }else {
            convertViewHolder = (ConvertViewHolder) view.getTag();
        }
        if (convertList != null && convertList.size()>0){
            Convert convert = convertList.get(position);
            convertViewHolder.title.setText(convert.getName());
            convertViewHolder.price.setText(convert.getConsume());
            convertViewHolder.remain.setText(convert.getRemain());
            if (convertViewHolder.button.isPressed()){
                int whiteColor = Color.argb(255,255,255,255);
                convertViewHolder.button.setTextColor(whiteColor);
            }else {
                int blueColor = Color.argb(255,0,0,255);
                convertViewHolder.button.setTextColor(blueColor);
            }
            convertViewHolder.imageView.setImageBitmap(null);
            imageLoader.displayImage(convert.getIcon(),convertViewHolder.imageView);
        }

        return view;
    }

    public void refreshListView(List<Convert> convertList){
        this.convertList = convertList;
        notifyDataSetChanged();
    }
}
